<?php

namespace Modules\Car\Http\Controllers\Api;
use App\Http\Resources\IllegalCollection;
use Illuminate\Support\Facades\DB;
use App\Helpers\ResponseData;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\Illegal;
use Modules\Car\Entities\IllegalRecord;
//违章
class IllegalsController extends Controller
{
    use ResponseData;
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function getUndealIllegal(Request $request){
        if ($request->userid) {
            $order_sn = CarOrder::query()->where('userid',$request->userid)->pluck('order_sn');
        }
        if ($request->customer_id) {
            $order_sn = CarOrder::query()->where('customer_id',$request->customer_id)->pluck('order_sn');
        }
        if($order_sn){
            $data = Illegal::query()->whereIn('order_sn',$order_sn)->get();
            return $this->success($data);
        }else{
            return $this->failed('未找到当前用户订单',201);
        }
    }
    public function list(Illegal $illegal,Request $request)
    {
        $size = $request->input('size',10);
        $scores = $request->input('scores',0);
        $mount = $request->input('mount','');
        $type = $request->input('type','');
        $name = $request->input('name','');
        $lt_mount = $request->input('lt_mount','');
        $category = $request->input('category','');
        $id = $request->input('id','');
        // $deal = $request->input('is_deal',0);
        $order_sn = $request->input('order_sn',null);
        if($request->filled('id')){
            $model = $illegal
                ->with('car','gear','order');
            if($request->filled('type')){
                $model = $model->where('type',$request->type);
            }
            $data = $model
                ->where('car_id',$id)->get();
            if($data){
                return $this->success($data);
            }

        }
        $user = $request->user();
        if(!$user){
            return $this->failed('权限不足,请联系管理员!',201);
        }

        $list = $illegal->query()
            ->where('plate_number','like','%'.$request->plate_number.'%')
            ->with('car','gear','order')
            ->when($order_sn,function($query)use($order_sn){
                $query->where('order_sn',$order_sn);
            })
            ->when($scores,function($query)use($scores){
                $query->where('scores',$scores);
            })
            // ->when($deal,function($query)use($deal){
            //     $query->where('is_deal',$deal);
            // })
            ->when($lt_mount,function($query)use($lt_mount){
                $query->where('amount','>',200);
            })
            ->when($mount,function($query)use($mount){
                $query->where('amount',$mount);
            })
            ->when($name,function($query)use($name){
                $query->where('car_name',$name);
            })
            ->when($category,function($query)use($category){
                $query->where('car_category',$category);
            });
        
        if($request->filled('type')){
            $list = $list->where('type',$request->type);
        }
        $list = $list ->orderByDesc('id')
            ->paginate($size);
        return $this->success($list);
    }
    // 获取违章记录次数
    public function getLog(Illegal $illegal,Request $request){
        $size = $request->input('size',20);

        $model = $illegal
            ->select('car_name','id','plate_number','car_id');
        if($request->filled('plate_number')){
            $model = $model->where('plate_number','like','%'.$request->plate_number.'%');
        }
        $collection = $model->groupBy('plate_number')
            ->having('plate_number','<>','')
            ->paginate($size);
//        return new IllegalCollection($collection);
//        return $this->success($collection);
        return IllegalCollection::collection($collection)->additional(['message'=>'success','code'=>200]);
    }
    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Illegal $illegal,Request $request)
    {
        $user = $request->user();
        $order = CarOrder::query()->where('order_sn',$request->order_sn)->first();
        if(!$order){
            return $this->failed('订单不存在!',201);
        }
        if(!$user){
            return $this->failed('权限不足,请联系管理员!',201);
        }
        $data = $request->all();
        $data['manager_id'] = $user->id;
        $data['manager_name'] = $user->nick_name;
        $data['car_series_id'] = $order->car_series_id;
        $data['gear_car_id'] = $order->gear_car_id;
        $data['phone'] = $order->phone;
        $data['fetch_time'] = $order->fetch_time;
        $data['return_time'] = $order->return_time;

        $illegal->fill($data);
        $illegal->save();
        return $this->success('保存成功');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit(Illegal $illegal,CarOrder $order,Request $request)
    {
        $data = $illegal->query()->with('car','gear')->find($request->id);
        if($data){
            $data->order = $order->where('order_sn', $data->order_sn)->first();
            return $this->success($data);
        }
        return $this->failed('未找到该车辆违章',201);
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Illegal $illegal,Request $request)
    {
        $user = $request->user();
        if(!$user){
            return $this->failed('权限不足,请联系管理员!',201);
        }
        $data = $illegal->query()
            ->where('id',$request->id)
            ->first();
//        if($user->department_id!=1){
//            $data = $illegal->query()
//                ->where('id',$request->id)
//                ->where('manager_id',$user->id)
//                ->first();
//        }else{
//            $data = $illegal->query()
//                ->where('id',$request->id)
//                ->first();
//        }


        if($data){
            if($request->filled('plate_number')){
                $data->plate_number = $request->plate_number;
            }
            if($request->filled('username')){
                $data->username = $request->nick_name;
            }
            if($request->filled('date')){
                $data->date = $request->date;
            }
            if($request->filled('scores')){
                $data->scores = $request->scores;
            }
            if($request->filled('amount')){
                $data->amount = $request->amount;
            }
            if($request->filled('status')){
                $data->status = $request->status;
            }
            if($request->filled('contents')){
                $data->contents = $request->contents;
            }
            if($request->filled('images')){
                $data->images = $request->images;
            }
            if($request->filled('changer')){
                $data->changer = $request->changer;
            }
            // if($request->filled('is_deal')){
            //     $data->is_deal = $request->is_deal;
            // }
            if($request->filled('voucher')){
                $data->voucher = $request->voucher;
            }
            if($request->filled('type')){
                $data->type = $request->type;
            }

            $data->save();
            return $this->success('更新成功!');
        }
        return $this->failed('未找到该车辆违章',201);
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy(Illegal $illegal,Request $request)
    {
        $user = $request->user();
        if(!$user){
            return $this->failed('权限不足,请联系管理员!',201);
        }
        $data = $illegal->query()
            ->where('car_id',$request->id)
            ->where('manager_id',$user->id)
            ->first();
        if($data){
            $data->query()->delete();
            return $this->success('删除成功!');
        }
        return $this->failed('删除失败!',201);
    }
}
